home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 14
/
Mac Magazin and MacEasy Magazine CD - Issue 14.iso
/
Online
/
NewsWatcher 2.0+jp1 folder
/
patch source
/
myTools.c
next >
Wrap
Text File
|
1995-07-15
|
6KB
|
313 lines
// NewsWatcher 2.0 "myTools.c"
// Copyright (C) 1994-1995 Mizutori Tetsuya
// 1994/5/24, 1994/7/15, 1995/2/8, 1995/2/21, 1995/7/14
#include "myTools.h"
// Global variables
//Boolean gOptionPressed = false; /* true if Option Key is pressed */
/***** Keyboard sensing *****/
#ifdef COMMENT
Apple Standard keyboard Layout
Examples:
Code Legend
0x00 A
0x01 S
0x06 Z
0x24 Return
0x30 Tab
0x31 Space
0x33 Delete
0x35 Escape
#endif /* COMMENT */
// k = keyboard scan code (0-127 or modifier keys)
static Boolean IsPressed ( unsigned short k )
{
unsigned char km[16];
switch ( k ) {
case cmdKey: k = 0x37; break;
case shiftKey: k = 0x38; break;
case alphaLock: k = 0x39; break;
case optionKey: k = 0x3A; break;
case controlKey: k = 0x3B; break;
default: break;
}
GetKeys( (void *) km );
return ( ( ( km[k>>3] >> (k & 7) ) ) & 1 != 0 );
}
/**** Kanji code conversion *****/
#ifdef COMMENT
Kanji code conversion
Added by Mizutori Tetsuya, 1994/5/24
JIS Sequence
Kanji-in: ESC + '$' + { 'B', '@', '(B' }
Kanji-out: ESC + '(' + { 'J', 'B', 'H' }
#endif /* COMMENT */
#define IN(x,a,b) (((a) <= (x)) && ((x) <= (b)))
/**** Kanji code check routines *****/
Boolean IsJIS ( unsigned short d )
{
unsigned short d1, d2;
d1 = BYTE_AT( &d, 0 );
d2 = BYTE_AT( &d, 1 );
if ( ! IN(d1,0x21,0x7e) ) return false;
if ( ! IN(d2,0x21,0x7e) ) return false;
return true;
}
Boolean IsSJIS ( unsigned short d )
{
unsigned short d1, d2;
d1 = BYTE_AT( &d, 0 );
d2 = BYTE_AT( &d, 1 );
if ( ! IN(d1,0x81,0x9e) && ! IN(d1,0xe0,0xec) ) return false;
if ( ! IN(d2,0x40,0x7e) && ! IN(d2,0x80,0xfc) ) return false;
return true;
}
Boolean IsEUC ( unsigned short d )
{
return ( ( d & 0x8080 ) == 0x8080 );
}
Boolean IsKanji ( char *p, char *pStart, char *pEnd )
{
if ( p < pStart ) return false;
if ( pEnd < p+2 ) return false;
return IsSJIS( WORD_AT( p ) );
}
/***** Kanji code conversion *****/
unsigned short EUC2JIS ( unsigned short d )
{
return ( d & 0x7f7f );
}
unsigned short JIS2EUC ( unsigned short d )
{
return ( d | 0x8080 );
}
unsigned short SJIS2JIS ( unsigned short d )
{
unsigned short d1,d2;
unsigned short x1,x2;
unsigned short x;
d1 = BYTE_AT( &d, 0 );
d2 = BYTE_AT( &d, 1 );
// if ( d1 == 0 ) return d;
x1 = d1 + d1 - ((d1 <= 0x9f) ? 0x00e1 : 0x0161);
if ( d2 < 0x9f ) x2 = d2 - ((d2 > 0x7f) ? 0x20 : 0x1f);
else { x2 = d2 - 0x7e; x1 += 1; }
BYTE_AT( &x, 0 ) = x1;
BYTE_AT( &x, 1 ) = x2;
return x;
}
unsigned short JIS2SJIS ( unsigned short d )
{
unsigned short d1,d2;
unsigned short x1,x2;
unsigned short x;
d1 = BYTE_AT( &d, 0 );
d2 = BYTE_AT( &d, 1 );
// if ( d1 == 0 ) return d;
x1 = ((d1-1)>>1) + ((d1 <= 0x5e) ? 0x71 : 0xb1);
x2 = d2 + ((d1 & 1) ? ((d2 < 0x60) ? 0x1f : 0x20) : 0x7e);
BYTE_AT( &x, 0 ) = x1;
BYTE_AT( &x, 1 ) = x2;
return x;
}
/***** Skip puncutuations or braces following to Kanji string *****/
long SkipOnBrace ( char *p, char *pStart, char *pEnd )
{
long skipCount;
register unsigned char *q, cp;
unsigned char romanBraces[16] = ".,;:?!)]}>-\0";
unsigned char kanjiBraces[64] = {
0xff,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0xff,0xff,
0x60,0xff,0xff,0x63,0x64,0xff,0x66,0xff,0x68,0xff,0x6a,0xff,0x6c,0xff,0x6e,0xff,
0x70,0xff,0x72,0xff,0x74,0xff,0x76,0xff,0x78,0xff,0x7a,0xff,0xff,0xff,0xff,0x00,
};
// The items of kanjiBraces[] may be assigned in a random order.
// You are allowed to eliminate the items whose value is 0xff.
// kanjiBraces[] array must have its last item as '\0'.
// So, you can also define: kanjiBraces[] = "ABCD...XYZ[\\]`cdfhjlnprtvxz"
// kanjiBraces[] includes punctuations and right braces in [0x8140 .. 0x817E]
// which follow the Kanji strings.
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 0x8140: Å@ÅAÅBÅCÅDÅEÅFÅGÅHÅIÅJÅKÅLÅMÅNÅO 0x40:-123456789ABCDEF
// 0x8150: ÅPÅQÅRÅSÅTÅUÅVÅWÅXÅYÅZÅ[Å\Å]Å^Å_ 0x50:0123456789ABCD--
// 0x8160: Å`ÅaÅbÅcÅdÅeÅfÅgÅhÅiÅjÅkÅlÅmÅnÅo 0x60:0--34-6-8-A-C-E-
// 0x8170: ÅpÅqÅrÅsÅtÅuÅvÅwÅxÅyÅzÅ{Å|Å}Å~** 0x70:0-2-4-6-8-A----*
// Sorry, these 2-byte characters are shown properly in Kanji font.
// if ( IsPressed(optionKey) || IsPressed(shiftKey) ) return 0;
if ( p < pStart ) return 0;
if ( pEnd <= p ) return 0;
if ( IsKanji(p,pStart,pEnd) ) {
if ( *p != 0x81 ) return 0;
cp = *(p+1);
q = kanjiBraces;
skipCount = 2;
} else {
cp = *p;
q = romanBraces;
skipCount = 1;
}
while ( *q != '\0' ) {
if ( cp == *q ) return skipCount;
q++;
}
return 0;
}
/**** Memory routines *****/
#include "memutil.h"
OSErr MyResizeHandle (void *handle, Size len)
{
char hState;
// Size oldLen;
OSErr err;
// oldLen = GetHandleSize(handle);
hState = MyHGetState(handle);
MyHUnlock(handle);
err = MySetHandleSize(handle, len);
MyHSetState(handle, hState);
return err;
}
/***** Resource Handling *****/
long gFillColumn = 0;
#define kFillColumnID 800
#define iTextBox 4
#define kMoveToFront (WindowPtr)-1L
#define kNilFilterProc nil
long GetFillColumn ( void )
{
DialogPtr dialog;
long data;
data = ( gFillColumn > 0 ? gFillColumn : 76 );
if ( ! IsPressed( optionKey ) ) return data;
dialog = GetNewDialog( kFillColumnID, nil, kMoveToFront );
if ( dialog != nil ) {
short item;
short iType;
Handle iHndl;
Rect iRect;
Str255 text;
OSErr err;
GetDialogItem( dialog, iTextBox, &iType, &iHndl, &iRect );
NumToString( data, text );
SetDialogItemText( iHndl, text );
SelectDialogItemText( dialog, iTextBox, 0, 32767 );
// ParamText( prompt, "\p", "\p", "\p" );
err = SetDialogDefaultItem( dialog, ok );
err = SetDialogCancelItem( dialog, cancel );
err = SetDialogTracksCursor( dialog, true );
ShowWindow( dialog );
do {
ModalDialog( kNilFilterProc, &item );
} while ( item != ok && item != cancel );
GetDialogItemText( iHndl, text );
DisposeDialog( dialog );
if ( item == ok ) StringToNum( text, &data );
}
if ( data < 10 ) data = 10;
gFillColumn = data;
return data;
}
#ifdef COMMENT
long GetFillColumn ( void )
{
StringHandle theHndl;
long data = 0;
theHndl = GetString( kFillColumnID );
if ( theHndl != nil ) {
StringToNum( *theHndl, &data );
DisposeHandle( (Handle) theHndl );
}
if ( data < 10 ) data = 10;
return data;
}
#endif /* COMMENT */
// end of program